package com.alfred.bus.controller;

import com.alfred.bus.service.CustomerService;
import com.alfred.bus.vo.CustomerVo;
import com.alfred.sys.domain.User;
import com.alfred.sys.service.UserService;
import com.alfred.sys.utils.DataGridView;
import com.alfred.sys.utils.ResultObj;
import com.alfred.sys.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * 客户管理
 * @author 张彤彤
 * @date 2024/3/1 15:18
 */
@RestController  //以json形式响应数据
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    private CustomerService customerService;

    /**
     * 加载客户列表返回DataGridView
     * @param customerVo
     * @return
     */
    @RequestMapping("loadAllCustomer")
    public DataGridView loadAllCustomer(CustomerVo customerVo){
        return this.customerService.queryAllCustomer(customerVo);
    }

    @Autowired
    private UserService userService;
    /**
     * 添加一个客户
     * @param customerVo
     * @return
     */
    @RequestMapping("addCustomer")
    public ResultObj addCustomer(CustomerVo customerVo){
        try{
            customerVo.setCreatetime(new Date());
            this.customerService.addCustomer(customerVo);
            UserVo userVo=new UserVo();
            userVo.setIdentity(customerVo.getIdentity());
            userVo.setLoginname(customerVo.getPhone());
            userVo.setRealname(customerVo.getCustname());
            userVo.setPhone(customerVo.getPhone());
            userVo.setSex(customerVo.getSex());
            userVo.setAddress(customerVo.getAddress());
            userVo.setPosition(customerVo.getCareer());
            userVo.setAvailable(1);
            userService.addUser(userVo);
            User user = userService.selectByIdentity(userVo);
            Integer[] khRole={6};
            userVo.setUserid(user.getUserid());
            userVo.setIds(khRole);
            userService.saveUserRole(userVo);
            return ResultObj.ADD_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }
    }

    /**
     * 修改一个客户
     * @param customerVo
     * @return
     */
    @RequestMapping("updateCustomer")
    public ResultObj updateCustomer(CustomerVo customerVo){
        try{
            this.customerService.updateCustomer(customerVo);
            return ResultObj.UPDATE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }
    }

    /**
     * 删除一个客户
     * @param customerVo
     * @return
     */
    @RequestMapping("deleteCustomer")
    public ResultObj deleteCustomer(CustomerVo customerVo){
        try {
            this.customerService.deleteCustomer(customerVo.getIdentity());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

    /**
     * 批量删除客户
     * @param customerVo
     * @return
     */
    @RequestMapping("deleteBatchCustomer")
    public ResultObj deleteBatchCustomer(CustomerVo customerVo){
        try{
            this.customerService.deleteBatchCustomer(customerVo.getIds());
            return ResultObj.DELETE_SUCCESS;
        }catch (Exception e){
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }

}
